import { ResPage, ResultData } from "@/api/interface/index";
import { PORT1 } from "@/api/config/servicePort";
import http from "@/api";

export namespace WorkOrder {
  export interface WorkOrderListItem {
    id: string; // 字符串类型以避免大数精度丢失
    workOrderCode: string;
    productModel: string;
    plannedQuantity: number;
    completedQuantity?: number;
    status: string;
    createdTime?: string | number[] | Date; // 支持多种时间格式
    updatedTime?: string | number[] | Date; // 支持多种时间格式
    routeCode?: string;
    productionLineCode?: string;
  }

  export interface CreateWorkOrderRequest {
    workOrderCode: string;
    productModel: string;
    plannedQuantity: number;
    routeCode?: string;
    productionLineCode?: string;
  }

  export interface SuspendWorkOrderRequest {
    suspendReason: string;
  }

  export interface CompleteWorkOrderRequest {
    completedQuantity: number;
  }

  export interface CloseWorkOrderRequest {
    closeReason: string;
  }

  export interface CancelWorkOrderRequest {
    cancelReason: string;
  }

  export interface WorkOrderListParams {
    current?: number;
    size?: number;
    workOrderCode?: string;
    productModel?: string;
    status?: string;
  }

  export interface BatchDeleteWorkOrderRequest {
    ids: string[];
  }
}

/**
 * @name 工单管理模块
 */
// 获取工单列表
export const getWorkOrderList = (params: WorkOrder.WorkOrderListParams) => {
  return http.get<ResPage<WorkOrder.WorkOrderListItem>>(`/workorders/list`, params);
};

// 创建工单
export const createWorkOrder = (params: WorkOrder.CreateWorkOrderRequest) => {
  return http.post<ResultData<number>>(`/workorders`, params);
};

// 获取工单详情
export const getWorkOrderById = (id: string) => {
  return http.get<ResultData<WorkOrder.WorkOrderListItem>>(`/workorders/${id}`);
};

// 下达工单
export const releaseWorkOrder = (id: string) => {
  return http.post<ResultData<void>>(`/workorders/${id}/release`);
};

// 开始工单
export const startWorkOrder = (id: string) => {
  return http.post<ResultData<void>>(`/workorders/${id}/start`);
};

// 暂停工单
export const suspendWorkOrder = (id: string, params: WorkOrder.SuspendWorkOrderRequest) => {
  return http.post<ResultData<void>>(`/workorders/${id}/suspend`, params);
};

// 恢复工单
export const resumeWorkOrder = (id: string) => {
  return http.post<ResultData<void>>(`/workorders/${id}/resume`);
};

// 完成工单
export const completeWorkOrder = (id: string, params: WorkOrder.CompleteWorkOrderRequest) => {
  return http.post<ResultData<void>>(`/workorders/${id}/complete`, params);
};

// 关闭工单
export const closeWorkOrder = (id: string, params: WorkOrder.CloseWorkOrderRequest) => {
  return http.post<ResultData<void>>(`/workorders/${id}/close`, params);
};

// 取消工单
export const cancelWorkOrder = (id: string, params: WorkOrder.CancelWorkOrderRequest) => {
  return http.post<ResultData<void>>(`/workorders/${id}/cancel`, params);
};

// 更新工单
export const updateWorkOrder = (id: string, params: WorkOrder.CreateWorkOrderRequest) => {
  return http.put<ResultData<void>>(`/workorders/${id}`, params);
};

// 批量删除工单
export const batchDeleteWorkOrder = (ids: string[]) => {
  return http.delete<ResultData<void>>(`/workorders/batch`, { data: { ids } });
};

// 删除单个工单
export const deleteWorkOrder = (id: string) => {
  return http.delete<ResultData<void>>(`/workorders/${id}`);
};
